package com.itcms.webservice.uuv.org;

import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

import com.itcms.common.util.DateUtil;
import com.itcms.webservice.util.AddSoapHeader;

public class Test {
	 private static final String webServiceConTimeout = "6000";  
	    private static final String webServiceRevTimeout = "6000";  
	public static void main(String[] args) {
		/*ESBSecurityToken esbSecurityToken = new ESBSecurityToken();
		esbSecurityToken.setUsername("OutNewAdmin");
		esbSecurityToken.setPassword("489D717670E16B00D9CA9EEDA037F529");
		esbSecurityToken.setTimestamp("2017-04-23 10:15:55");
		UUVOrgServices service = new UUVOrgServices();  
		UUVOrgServicesSoap soap = service.getUUVOrgServicesSoap(); 
		XMLGregorianCalendar beginLastUpdateDate = dateToXmlDate(DateUtil.getDate("2010-01-01", DateUtil.DATE_FORMAT));
		XMLGregorianCalendar endLastUpdateDate = dateToXmlDate(DateUtil.getDate("2017-04-24", DateUtil.DATE_FORMAT));
		System.out.println(soap.getUUVLocalOrgList(beginLastUpdateDate, endLastUpdateDate));*/
		 test1();
	}
	public static void test3() {
		UUVOrgServices services = new UUVOrgServices();  
		UUVOrgServicesSoap soap = services.getUUVOrgServicesSoap();
	        //以上什么意思请参考：http://learning.iteye.com/admin/blogs/1333223  
	        Client clientProxy = ClientProxy.getClient(soap);//通过目标ws获取代理  
	        //注入拦截器，getOutInterceptors代表调用服务端时触发,getInInterceptors就是被调用才触发  
	        AddSoapHeader ash = new AddSoapHeader();
	        clientProxy.getOutInterceptors().add(ash);  
	        // 超时时间设置  
	        HTTPConduit http = (HTTPConduit) clientProxy.getConduit();  
	        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();  
	        httpClientPolicy.setConnectionTimeout(Integer  
	                .valueOf(webServiceConTimeout));  
	        httpClientPolicy.setReceiveTimeout(Integer  
	                .valueOf(webServiceRevTimeout));  
	        httpClientPolicy.setAllowChunking(false);  
	        http.setClient(httpClientPolicy);  
	        //以上插入点超时设置方式  
	        //下面这行代码是具体调用服务段的deleteTeskTask（）  
	    	XMLGregorianCalendar beginLastUpdateDate = dateToXmlDate(DateUtil.getDate("2010-01-01", DateUtil.DATE_FORMAT));
			XMLGregorianCalendar endLastUpdateDate = dateToXmlDate(DateUtil.getDate("2017-04-24", DateUtil.DATE_FORMAT));
			System.out.println(soap.getUUVLocalOrgList(beginLastUpdateDate, endLastUpdateDate)); 
	}
	public static void test1() {
		// TODO Auto-generated method stub
		JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();  
        soapFactoryBean.setAddress("http://172.16.84.20:32069/BizTalkESB/10002.asmx");   
        soapFactoryBean.getInInterceptors().add(new LoggingInInterceptor());   
        soapFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());   
        // 添加拦截器  
        soapFactoryBean.getOutInterceptors().add(new AddSoapHeader());  
        soapFactoryBean.setServiceClass(UUVOrgServicesSoap.class);  
        Object o = soapFactoryBean.create();   
        UUVOrgServicesSoap uuvOrgServicesSoap = (UUVOrgServicesSoap)o;   
        try {  
        	XMLGregorianCalendar beginLastUpdateDate = dateToXmlDate(DateUtil.getDate("2010-01-01", DateUtil.DATE_FORMAT));
    		XMLGregorianCalendar endLastUpdateDate = dateToXmlDate(DateUtil.getDate("2017-04-24", DateUtil.DATE_FORMAT));
    		ArrayOfADDepartmentInfo arrayOfADDepartmentInfo = uuvOrgServicesSoap.getUUVLocalOrgList(beginLastUpdateDate, endLastUpdateDate);
    		List<ADDepartmentInfo> departmentInfos = arrayOfADDepartmentInfo.getADDepartmentInfo();
    		System.out.println(departmentInfos.size());
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
	}
	public static final JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  

	private static void test2() {
		// TODO Auto-generated method stub

		AddSoapHeader ash = new AddSoapHeader();
		ArrayList list = new ArrayList<Interceptor>();
		// 添加soap header 信息
		list.add(ash);
//	　	 添加soap消息日志打印
		list.add(new org.apache.cxf.interceptor.LoggingOutInterceptor());
		// 注入拦截器，getOutInterceptors代表调用服务端时触发,getInInterceptors就是被调用才触发
		factory.setOutInterceptors(list);
		factory.setServiceClass(UUVOrgServicesSoap.class);// 实例化ws
		factory.setAddress("http://172.16.84.20:32069/BizTalkESB/10002.asmx");
		Object obj = factory.create();
		System.out.println(obj == null ? "NULL" : obj.getClass().getName());
		UUVOrgServicesSoap uuvOrgServicesSoap = (UUVOrgServicesSoap) obj;
		// 下面这行代码是具体调用服务段的deleteTeskTask（）
		XMLGregorianCalendar beginLastUpdateDate = dateToXmlDate(DateUtil.getDate("2010-01-01", DateUtil.DATE_FORMAT));
		XMLGregorianCalendar endLastUpdateDate = dateToXmlDate(DateUtil.getDate("2017-04-24", DateUtil.DATE_FORMAT));
		ArrayOfADDepartmentInfo arrayOfADDepartmentInfo = uuvOrgServicesSoap.getUUVLocalOrgList(beginLastUpdateDate, endLastUpdateDate);
		List<ADDepartmentInfo> departmentInfos = arrayOfADDepartmentInfo.getADDepartmentInfo();
		System.out.println(departmentInfos.size());

	}
	
	/** 
     * 将Date类转换为XMLGregorianCalendar 
     * @param date 
     * @return  
     */  
    public static XMLGregorianCalendar dateToXmlDate(Date date){  
            Calendar cal = Calendar.getInstance();  
            cal.setTime(date);  
            DatatypeFactory dtf = null;  
             try {  
                dtf = DatatypeFactory.newInstance();  
            } catch (DatatypeConfigurationException e) {  
            }  
            XMLGregorianCalendar dateType = dtf.newXMLGregorianCalendar();  
            dateType.setYear(cal.get(Calendar.YEAR));  
            //由于Calendar.MONTH取值范围为0~11,需要加1  
            dateType.setMonth(cal.get(Calendar.MONTH)+1);  
            dateType.setDay(cal.get(Calendar.DAY_OF_MONTH));  
            dateType.setHour(cal.get(Calendar.HOUR_OF_DAY));  
            dateType.setMinute(cal.get(Calendar.MINUTE));  
            dateType.setSecond(cal.get(Calendar.SECOND));  
            return dateType;  
        }  
}
